package cn.topcodes.tcsf.admin.web.controller.impl;

import cn.topcodes.tcsf.admin.commons.api.security.ShiroUtil;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.commons.api.controller.generic.GenericApiController;
import cn.topcodes.tcsf.admin.domain.entity.SysPerm;
import cn.topcodes.tcsf.admin.domain.entity.SysUrl;
import cn.topcodes.tcsf.admin.domain.entity.SysUser;
import cn.topcodes.tcsf.admin.domain.form.NestableSort;
import cn.topcodes.tcsf.admin.domain.form.SysPermForm;
import cn.topcodes.tcsf.admin.domain.view.SysPermDetailVo;
import cn.topcodes.tcsf.admin.service.res.SysPermService;
import cn.topcodes.tcsf.admin.service.res.SysUrlService;
import cn.topcodes.tcsf.admin.service.org.SysUserService;
import cn.topcodes.tcsf.admin.web.controller.SysPermApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin/sys-perm")
public class SysPermApiController extends GenericApiController<SysPerm, SysPermDetailVo, SysPermForm> implements SysPermApi {

    @Autowired
    private SysUserService sysUserService;

	@Autowired
	private SysPermService sysPermService;

	@Autowired
	private SysUrlService sysUrlService;

	@RequestMapping("")
	public String index(Model model) {
		List<SysPerm> parentPerms = this.sysPermService.getRootPerms();
		model.addAttribute("parentPerms",parentPerms);
		List<SysUrl> urls = this.sysUrlService.findAll();
		model.addAttribute("urls",urls);
		return "admin/sys-perm";
	}

	@ResponseBody
	@RequestMapping("create-perm")
	public ApiResponseEntity<Void> create(
			@RequestParam(name = "menus[]",required = false) Long[] menus,
			@RequestParam(name = "urls",required = false) Long[] urls,
			@Valid SysPermForm form, BindingResult result) {
		SysPerm perm = this.form2Entity(form);
		this.sysPermService.create(perm,menus,urls);
		return ApiResponseEntity.buildOK();
	}

    @Override
    protected boolean validateOperation(String password) {
        Long userId = ShiroUtil.getUserId();
        SysUser user = this.sysUserService.findOne(userId);
        if(user == null) {
            throw new EntityNotFoundException("管理用户不存在");
        }
        return this.sysUserService.isPwdEqual(password,user);
    }

	@ResponseBody
	@RequestMapping("update-perm")
	public ApiResponseEntity<Void> update(
			@RequestParam(name = "menus[]",required = false) Long[] menus,
			@RequestParam(name = "urls",required = false) Long[] urls,
			@Valid SysPermForm form, BindingResult result) {
		SysPerm perm = this.form2Entity(form);
		this.sysPermService.update(perm,menus,urls);
		return ApiResponseEntity.buildOK();
	}

    @ResponseBody
    @RequestMapping(value = "save-sort",method = RequestMethod.POST)
    public ApiResponseEntity saveSort(@RequestBody(required = true) List<NestableSort> sort) {
        this.sysPermService.saveSort(sort);
        return ApiResponseEntity.buildOK();
    }

    @Override
    protected void entity2VoDiff(SysPerm entity, SysPermDetailVo vo) {
        vo.setMenuIds(this.sysPermService.getMenus(entity.getId()));
        vo.setUrlIds(this.sysPermService.getUrls(entity.getId()));
    }
}
